# Contributor: The one with the braid <info@braid.business>
# Maintainer: The one with the braid <info@braid.business>
pkgname=fwallet
pkgver=1.2.0
pkgrel=0
pkgdesc="A beautiful cross-platform wallet application for your transport tickets, discount cards and subscriptions."
url="https://gitlab.com/TheOneWithTheBraid/f_wallet"
arch="aarch64 x86_64"	# flutter
license="EUPL-1.2"
# xdg-user-dirs: pub/path_provider
depends="
	xdg-user-dirs
	"
makedepends="
	ca-certificates
	flutter-desktop
	libsecret-dev
	patchelf
	"
source="
	https://gitlab.com/TheOneWithTheBraid/f_wallet/-/archive/${_rev:-"v$pkgver"}/f_wallet-${_rev:-v"$pkgver"}.tar.gz

	system-flutter.patch
	no-werror.patch
	flutter-3.24.patch
	"
builddir="$srcdir/f_wallet-${_rev:-"v$pkgver"}"
# net: pub dependencies
# !check: no tests
options="net !check"

export _pkgorg="business.braid.f_wallet"
export _pkgexec="f_wallet"

case "$CARCH" in
	aarch64) _flutter_arch="arm64" ;;
	x86_64) _flutter_arch="x64" ;;
esac

export PUB_CACHE="$srcdir/pub_cache"

export CFLAGS="$CFLAGS -O2 -Wno-error -Wno-unknown-warning-option -Wno-unused-command-line-argument"
export CXXFLAGS="$CXXFLAGS -O2 -Wno-error -Wno-unknown-warning-option -Wno-unused-command-line-argument"

export CC=clang
export CXX=clang++
export AR=llvm-ar
export NM=llvm-nm
export LD=clang++

# FIXME: i'm sorry. workarounds https://gitlab.alpinelinux.org/alpine/aports/-/issues/16556
real_so_path() {
	local so="$1"
	shift
	while [ $# -gt 0 ]; do
		[ -e "$1"/$so ] && realpath "$1/$so" && return 0
		shift
	done
	error "$so: path not found"
	return 0
}

prepare() {
	default_prepare

	flutter pub get --enforce-lockfile
	flutter gen-l10n

	# if there is a binary in the pub_cache, we sure did not build it.
	for elf in $(scanelf -RA -F "%F" "$PUB_CACHE"); do
		rm -f "$elf"
	done
}

build() {
	flutter build linux -v --release \
		--dart-define=FWALLET_IS_STABLE=true \
		--dart-define=FWALLET_VERSION=$pkgver

	patchelf --set-rpath '$ORIGIN/lib' build/linux/"$_flutter_arch"/release/bundle/$_pkgexec
	for _elf in build/linux/"$_flutter_arch"/release/bundle/lib/lib*.so; do
		# running patchelf on libapp (the AOT snapshot of the whole Dart codebase)
		# breaks it with "[FATAL:flutter/runtime/dart_vm_initializer.cc(89)]
		# Error while initializing the Dart VM: Invalid vm isolate snapshot seen".
		#
		# it has no rpath and only links to libc soname anyway. 🤷‍♀️
		[ "$(basename "$_elf")" != "libapp.so" ] || continue

		patchelf --set-rpath '$ORIGIN' "$_elf"
	done
}

package() {
	local bundle="$builddir"/build/linux/$_flutter_arch/release/bundle

	install -Dm755 "$bundle"/$_pkgexec "$pkgdir"/usr/lib/$pkgname/$_pkgexec
	cp -rv "$bundle"/lib "$pkgdir"/usr/lib/$pkgname/lib
	cp -rv "$bundle"/data "$pkgdir"/usr/lib/$pkgname/data

	mkdir -p "$pkgdir"/usr/bin
	ln -sv /usr/lib/$pkgname/$_pkgexec "$pkgdir"/usr/bin/$_pkgexec

	install -Dm644 linux/$_pkgorg.desktop "$pkgdir"/usr/share/applications/$_pkgorg.desktop
	install -Dm644 assets/logo/logo-circle.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/$_pkgorg.svg
}

sha512sums="
146a3af56bf6b60ede62f1a28939285affdfd16b99508de9f16d4da3fdd9ef67696a41a0dacb5df3c66a968b84ea6bb039748a62520eb45c95fdbb510a1122f1  f_wallet-v1.2.0.tar.gz
91014708ab7bb81518ce93db0612209d6479dfa503064d9e9d3186cd5e861165a3f63e286d271d5aac8a1d676868fc3acf51648ac01c16a215062a317ccb6623  system-flutter.patch
1d2b7d28a09e26ed48d89aa5f5dffb738ba367fa2760b12980225239705e16fa1ec89901d7aecf26c8527de721a5f50f50cc77ea009cd5f25f219d1af8730ed8  no-werror.patch
5a7d3830810ec8e5ef6d3ed70a981ebf09a1d8413f8e418bb888d7d006f40758ad06c8c49cc1710a4a8756bb10afe83314b01317769fe42e8cba2069e095311f  flutter-3.24.patch
"
